package com.luoye.lj.platform.service;

import com.luoye.lj.platform.model.UserModel;
import com.luoye.lj.platform.model.domain.AuthInfo;
import com.luoye.lj.platform.model.domain.muser.MUserDetail;
import com.luoye.lj.platform.model.enums.UserStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

	protected static final Logger LOG = LoggerFactory.getLogger(CustomUserDetailsService.class.getSimpleName());

    @Autowired
    private IMuserservice muserservice;

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		LOG.info("j_username->"+username);
		UserDetails user = null;  
		  
        try {
        	// 搜索数据库以匹配用户登录名.这里固定用户名和密码
            MUserDetail mUserDetail=muserservice.getUserDetail(username);
            LOG.info("mUserDetail->"+mUserDetail);
            if (mUserDetail != null&&mUserDetail.getStatus()== UserStatus.OK.getCode()) {
                user = new UserModel(username, mUserDetail.getPasswd(), getAuthorities(mUserDetail.getAuthInfoList()), mUserDetail);
            }
        } catch (Exception e) {
        	LOG.warn("Error in loadUserByUsername->"+e.getMessage(),e);
            throw new UsernameNotFoundException("Error in retrieving user");
        }
        if(user==null){
            LOG.info("用户没有找到");
            throw new UsernameNotFoundException("用户没有找到");
        }
  
        return user;  
	}
    /**
     * 获得访问角色权限
     *
     * @param auths
     * @return
     */
    public Collection<GrantedAuthority> getAuthorities(List<AuthInfo> auths) {
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);
        if(auths!=null){
            int count=auths.size();
            for(int i=0;i<count;i++){
                AuthInfo authInfo=auths.get(i);
                authList.add(new SimpleGrantedAuthority(authInfo.getAuthname()));
                LOG.debug("auth->"+authInfo.getAuthname());
            }
        }
        return authList;
    }

    /**
     * 获得访问角色权限 
     *  
     * @param auths
     * @return 
     */  
    public Collection<GrantedAuthority> getAuthorities(String auths) {  
    	List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2); 
    	if(auths!=null){
	    	String [] authsStrs=auths.split(",");       
	    	if(authsStrs.length>0){
	    		for(String auth:authsStrs){
	    			authList.add(new SimpleGrantedAuthority(auth)); 
	    			LOG.debug("auth->"+auth);
	    		}
	       	}
    	}
        return authList;  
    }  
}
